Fix #10846: [Squirrel] Ensure sqvector size does not overflow #10848
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #10846
Motivation / Problem
array(4611686018427387904)
implies allocating 4611686018427387904 * 16 (size ofSQObjectPtr
) and that is 0 in 64bit world.And as there is no check for overflow we happily allocate 0 bytes of memory and use it like if we allocated the expected number of elements.
Description
Check for overflow of
newsize * sizeof(T)
when reallocatingsqvector
.As
sqvector
is deep inside squirrel we don't have access to the VM and the only way to signal the error is throwing an exception. The only catchable exception without adding special handling inside squirrel itself, SQVM catches...
and rethrows, isScript_FatalError
so I used it.Adding overflow check in https://github.com/OpenTTD/OpenTTD/blob/master/src/3rdparty/squirrel/squirrel/sqbaselib.cpp#L217 could work for
array()
but would not protect when resizing the array after its creation, like inLimitations
Squirrel compiler also uses
sqvector
but it only catchesSQChar *
, so throwing something else might be an issue. Anyway compiler is less likely to try to allocate enough to trigger an overflow.Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.